package com.qf.controller;

import com.qf.dto.R;
import com.qf.exceptions.MyBaseException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class MyExceptionHandler {



    @ExceptionHandler(MyBaseException.class)
    public R processMyBaseException(MyBaseException e) {
        e.printStackTrace();
        return R.setError(e.getCode(),e.getMessage(),null);
    }



    @ExceptionHandler(ArithmeticException.class)
    public R processArithmeticException(ArithmeticException e) {
        e.printStackTrace();
        System.err.println("算数异常");
        return R.setError(null);
    }

    /**
     * 声明当前方法是处理Exception 类型的异常的
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R processException(Exception e) {
        e.printStackTrace();
        System.err.println("未知异常发生了");
        return R.setError(null);
    }

}
